package com.sne.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.sne.sys.entity.SysUser;
import com.sne.sys.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 处理新增和更新的基础数据填充
 *
 * @author fanghh
 * @since 2020-05-02
 */
@Slf4j
@Component
public class MetaHandler implements MetaObjectHandler {

    @Autowired
    private ISysUserService iSysUserService;

    @Override
    public void insertFill(MetaObject metaObject) {
        SysUser currentUser = iSysUserService.getCurrentUser();
        this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("createUserId", currentUser.getId(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        SysUser currentUser = iSysUserService.getCurrentUser();
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setFieldValByName("updateUserId", currentUser.getId(), metaObject);
    }

}
